package com.example.firstdemo.ui.screens.pages.problem.problemList

import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavOptionsBuilder
import androidx.navigation.compose.composable
import com.example.firstdemo.ui.screens.pages.problem.problemDetail.navigateToProblemDetail
import kotlinx.serialization.Serializable

@Serializable
object ProblemList

fun NavController.navigateToProblemList(builder: NavOptionsBuilder.() -> Unit = {}) {
    navigate(ProblemList, builder)
}

fun NavGraphBuilder.problemListDestination(
    padding: PaddingValues = PaddingValues(0.dp),
    navController: NavController,
) {
    composable<ProblemList>(
        enterTransition = { fadeIn() },
        exitTransition = { fadeOut() },
        popEnterTransition = { fadeIn() },
        popExitTransition = { fadeOut() }
    ) {
        val viewModel: ProblemListViewModel = viewModel()
        val uiState = viewModel.uiState.collectAsStateWithLifecycle().value
        ProblemListScreen(
            uiState,
            padding = padding,
            onGetProblemList = viewModel::getProblemList,
            onGotoDetail = { passTime, uid, stationName ->
                navController.navigateToProblemDetail(passTime, uid, stationName)
            }
        )
    }
}